<template>
  <el-dialog
    :title="$t('message.viewUserInfo')"
    :visible.sync="visible"
    :append-to-body="true">
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
      <el-form-item :label="$t('message.userName')" prop="userName">
        <el-input v-model="dataForm.userName" :placeholder="$t('message.loginAccount')" disabled></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.email')" prop="email">
        <el-input v-model="dataForm.email" :placeholder="$t('message.email')" disabled></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.phoneNo')" prop="mobile">
        <el-input v-model="dataForm.mobile" :placeholder="$t('message.phoneNo')" disabled></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.company')" prop="company">
        <el-input v-model="dataForm.company" :placeholder="$t('message.company')" disabled></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.contact')" prop="contact">
        <el-input v-model="dataForm.contact" :placeholder="$t('message.contact')" disabled></el-input>
      </el-form-item>
      <el-form-item :label="$t('message.role')" size="mini" prop="roleIdList">
        <el-checkbox-group v-model="dataForm.roleIdList" disabled>
          <el-checkbox v-for="role in roleList" :key="role.roleId" :label="role.roleId">{{ role.roleName }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item :label="$t('message.status')" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status" disabled>
          <el-radio :label="0">{{$t('message.disable')}}</el-radio>
          <el-radio :label="1">{{$t('message.ok')}}</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('message.close')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        roleList: [],
        dataForm: {
          userName: '',
          password: '',
          comfirmPassword: '',
          salt: '',
          email: '',
          mobile: '',
          company: '',
          contact: '',
          roleIdList: [],
          status: 1
        }
      }
    },
    computed: {
      userName: {
        get () { return this.$store.state.user.name }
      },
      mainTabs: {
        get () { return this.$store.state.common.mainTabs },
        set (val) { this.$store.commit('common/updateMainTabs', val) }
      }
    },
    methods: {
      // 初始化
      init () {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        this.$http({
          url: this.$http.adornUrl(`/sys/user/getUserInfo`),
          method: 'post',
          data: this.$http.adornData({})
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.userName = data.user.username
            this.dataForm.salt = data.user.salt
            this.dataForm.email = data.user.email
            this.dataForm.mobile = data.user.mobile
            this.dataForm.roleIdList = data.user.roleIdList
            this.dataForm.status = data.user.status
            this.dataForm.company = data.user.company
            this.dataForm.contact = data.user.contact
            this.roleList = data.roleList
          } else {
            this.$message.error(this.$t('message.failure'))
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/sys/user/password'),
              method: 'post',
              data: this.$http.adornData({
                'password': this.dataForm.password,
                'newPassword': this.dataForm.newPassword
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: this.$t('message.success'),
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

